﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProgNetComponents.Wcf.AppUpdate
{
    /// <summary>
    /// AppUpdateForm
    /// </summary>
    public partial class AppUpdateForm : Form
    {
        /// <summary>
        /// Prevents a default instance of the <see cref="AppUpdateForm"/> class from being created.
        /// </summary>
        private AppUpdateForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Updater
        /// </summary>
        BaseAppUpdater Updater;

        /// <summary>
        /// Initializes a new instance of the <see cref="AppUpdateForm"/> class.
        /// </summary>
        /// <param name="file">File with update info <see cref="BaseAppUpdater"/></param>
        public AppUpdateForm(string file)
        {
            Updater = new BaseAppUpdater();
            Updater.FromString(file.ReadFile());
            this.Text = Updater.AppName;

            InitializeComponent();

            this.label1.Text = "Uaktualnianie {0} do wersji {1}".FormatMe(Updater.AppName, Updater.Version);
            Updater.TaskError += new EventHandler(updater_TaskError);
            Updater.TaskFinished += new EventHandler(Updater_TaskFinished);
            Updater.CopyProgress += new Tools.FileTools.FileCopyProgress(updater_CopyProgress);
            Updater.Update();
        }

        void Updater_TaskFinished(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Updater_s the copy progress.
        /// </summary>
        /// <param name="fileLength">Length of the file.</param>
        /// <param name="bytesCopied">The bytes copied.</param>
        void updater_CopyProgress(long fileLength, long bytesCopied)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<long, long>(updater_CopyProgress), fileLength, bytesCopied);
                return;
            }
            this.progressBar1.Value = (int)(bytesCopied / fileLength) * 100;
        }

        /// <summary>
        /// Handles the TaskError event of the updater control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void updater_TaskError(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<object, EventArgs>(updater_TaskError), sender,e);
                return;
            }
            List<string> msg = new List<string>();
            foreach (Exception ex in Updater.Exceptions)
                msg.Add(ex.Message);
            MessageBox.Show(msg.AsString(Environment.NewLine), "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
